package model;

import entity.CardEntity;

public class ReadCard {
	CardEntity card;

	public ReadCard(String dataCard) {
		card = new CardEntity();

	}

	public ReadCard() {
		card = new CardEntity();
	}

	public CardEntity read(String dataCard) {
		CardEntity card = new CardEntity();

		String tmp1[] = dataCard.split(":TRACK");
						
		String track1 = tmp1[1];
		String track2 = tmp1[2];
		String track3 = tmp1[3];
						
		card.setTrack1(this.getTrack(track1));
		card.setTrack2(this.getTrack(track2));
		card.setTrack3(this.getTrack(track3));
		card.setPan(this.getPan(card.getTrack2()));
		card.setValidate(this.getValidate(card.getTrack2()));

		
		return card;
	}

	/*------------------------------------------------------------------------
	 * 
	 ------------------------------------------------------------------------*/
	public String getTrack(String track){	
		try {
			track = track.substring(2);
		} catch (Exception e) {			
			
		}
		return track;
	}
	
	
	public String getPan(String track2){
		String pan = "";
		try {
			String tmp2[] = track2.split("=");
			pan = tmp2[0];
		} catch (Exception e) {			
			
		}

		return pan;
	}
	
	public String getValidate(String track2){
		String validate = "";
		try {
			String tmp2[] = track2.split("=");
			validate = tmp2[1].substring(2, 4) + "/" + tmp2[1].substring(0, 2)  ;
		} catch (Exception e) {			
			
		}

		return validate;
	}
	
	/*------------------------------------------------------------------------
	 * 
	 ------------------------------------------------------------------------*/
	
	

	public CardEntity getCard() {
		return card;
	}

	public void setCard(CardEntity card) {
		this.card = card;
	}

}
